"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.dict.type.schemas.out import DictTypeCompleteOutSchema
from src.business.admin.system.dict.type.schemas.validate import (
    DictTypeCreateValidateSchema,
    DictTypeDeleteValidateSchema,
    DictTypeListSearchValidateSchema,
    DictTypeUpdateStatusValidateSchema,
    DictTypeUpdateValidateSchema,
)
from src.business.admin.system.dict.type.service import DictTypeService
from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetUserNameFromAccessTokenDependencies,
)
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_dict_type(
    dict_type_id: Annotated[int, Query(..., ge=1, description="字典类型ID")],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> DictTypeCompleteOutSchema:
    """查询单个字典类型的路由依赖

    Args:
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
        dict_type_id (Annotated[int, Query, optional): 字典类型ID

    Returns:
        DictTypeCompleteOutSchema: 字典类型信息
    """
    dict_type = await DictTypeService.get_dict_type_use_id(
        db_session,
        dict_type_id,
        raise_disabled_exception=False,
        raise_expired_exception=False,
    )
    return dict_type


async def route_depend_list_dict_type(
    search_schema: DictTypeListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[DictTypeCompleteOutSchema], int]:
    """查询字典类型完整信息列表的路由依赖

    Args:
        search_schema (Annotated[TenantListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[DictTypeCompleteOutSchema], int]: 查询结果
    """
    # 注解
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await DictTypeService.list_search_complete_dict_type(
        db_session, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_create_dict_type(
    create_schema: DictTypeCreateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建字典类型的路由依赖

    Args:
        create_schema (DictTypeCreateValidateSchema): 创建所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        int: 创建实例对应的主键
    """
    # 创建操作
    created_id = await DictTypeService.create_dict_type(
        db_session, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_dict_type(
    update_schema: DictTypeUpdateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改的路由依赖

    Args:
        update_schema (DictTypeUpdateValidateSchema): 修改所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await DictTypeService.update_dict_type(
        db_session, update_schema, updater_username=username
    )


async def route_depend_update_dict_type_status(
    update_schema: DictTypeUpdateStatusValidateSchema,
    username: Annotated[
        str,
        Depends(GetUserNameFromAccessTokenDependencies.from_header),
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改字典类型状态的路由依赖

    Args:
        update_schema (DictTypeUpdateStatusValidateSchema): 修改所需的参数
        username: token对应的用户名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await DictTypeService.update_dict_type_status(
        db_session, update_schema, updater_username=username
    )


async def route_depend_delete_dict_type(
    delete_schema: DictTypeDeleteValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除字典类型的路由依赖

    Args:
        delete_schema (DictTypeDeleteValidateSchema): 删除所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await DictTypeService.delete_dict_type(db_session, delete_schema)
